package com.itheima.paydemo.Ali;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.common.models.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author by jojo
 * @Date 2022/4/8
 * @Description
 */
@RestController
@RequestMapping("alipayeasy")
public class AliEasyPayController {

    @Autowired
    Config config;

    @GetMapping("payeasy/{orderNO}")
    public AlipayTradePrecreateResponse pay(@PathVariable("orderNO") String orderNO){
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
                    .preCreate("killerqueen.....",
                            orderNO,
                            "0.1");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
            return response;
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @GetMapping("query/{orderNO}")
    public AlipayTradeQueryResponse query(@PathVariable("orderNO") String orderNO){
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeQueryResponse response = Factory.Payment.Common()
                    .query(orderNO);
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
            return response;
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @GetMapping("refund/{orderNO}")
    public AlipayTradeRefundResponse refund(@PathVariable("orderNO") String orderNO){
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeRefundResponse response = Factory.Payment.Common()
                    .refund(orderNO, "0.1");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
            return response;
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @GetMapping("queryRefund/{orderNO}/{outNo}")
    public AlipayTradeFastpayRefundQueryResponse queryRefund(
            @PathVariable("orderNO") String orderNO,
            @PathVariable("outNo") String outNo){
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeFastpayRefundQueryResponse response = Factory.Payment.Common()
                    .queryRefund(orderNO, outNo);
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
            return response;
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
